//
//  AgreeViewController.m
//  WXOpenIMSampleRelease
//
//  Created by 中矿科技有限公司 on 16/5/24.
//  Copyright © 2016年 taobao. All rights reserved.
//

#import "AgreeViewController.h"
#import "UserLeaderCell.h"


@interface AgreeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *opinionLabel;
@property (weak, nonatomic) IBOutlet UITextView *tapinionText;
@property (weak, nonatomic) IBOutlet UILabel *selLeadertext;
@property(nonatomic,strong)UICollectionView *userleaderCV;
@property (weak, nonatomic) IBOutlet UITextView *opinionTextView;
@property(nonatomic,strong)AFHTTPData *http;

@property(nonatomic,assign,getter = isReject) BOOL reject;

@end
static NSString *leaderCellID =@"LeaderCell";
@implementation AgreeViewController
{
    NSIndexPath *lastIndexPath;
    NSInteger i;
    UILabel *opinionPlaceholder;
    MBProgressHUD * HUD;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    
    _http =[[AFHTTPData alloc]init];
    
    [self customUI];
    
}
-(void)customUI
{
    self.navigationItem.title =self.navtitle;
    
    self.automaticallyAdjustsScrollViewInsets =NO;
    //初始化UICollectionView
    UICollectionViewFlowLayout *flowLayout =[[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize =CGSizeMake(55, 55);
    self.userleaderCV =[[UICollectionView alloc]initWithFrame:CGRectMake(10,244+10,SCREEN_WIDTH-20 , 200) collectionViewLayout:flowLayout];
    if ([UIScreen mainScreen].bounds.size.height ==480.0) {
    self.userleaderCV.frame = CGRectMake(10, 244+10,SCREEN_WIDTH-20 , SCREEN_HEIGHT/4-18);
    }
    self.userleaderCV.backgroundColor =[UIColor whiteColor];
    [self.userleaderCV registerNib:[UINib nibWithNibName:@"UserLeaderCell" bundle:nil] forCellWithReuseIdentifier:leaderCellID];
    self.userleaderCV.delegate =self;
    self.userleaderCV.dataSource =self;
    [self.view addSubview:self.userleaderCV];
    self.userleaderCV.hidden =YES;
    self.selLeadertext.hidden =YES;
    self.nextUserID =@"0";
    

    

    //审批意见...
    self.opinionTextView.delegate =self;
    self.opinionTextView.layer.borderColor = UIColor.grayColor.CGColor;
    self.opinionTextView.layer.borderWidth = 1;
    self.opinionTextView.layer.cornerRadius = 6;
    self.opinionTextView.layer.masksToBounds = YES;
    opinionPlaceholder =[[UILabel alloc]initWithFrame:CGRectMake(3, 3, 100, 30)];
    opinionPlaceholder.enabled = NO;
    opinionPlaceholder.text = @"在此输入备注(非必填) :";
    opinionPlaceholder.font =  [UIFont systemFontOfSize:13];
    opinionPlaceholder.textColor = [UIColor lightGrayColor];
    [opinionPlaceholder sizeToFit];
    [self.opinionTextView addSubview:opinionPlaceholder];
    _reject =NO;
   // NSArray *actionCodeArray =@[@"ContractFlow.DX.Reject",@"ContractFlow.ZJ.Reject",@"ContractFlow.ZJ.Audit",@"TransferFlow.DX.Reject",@"TransferFlow.CW.Reject",@"TransferFlow.FG.Reject",@"TransferFlow.FG.Audit"];
//    for (NSString *str in actionCodeArray) {
        if ([self.actionCode hasSuffix:@"Reject"]) {
            opinionPlaceholder.text = @"在此输入备注(必填) :";
            _reject =YES;
        }
   // }
    
}

// TextView Delegate
- (void) textViewDidChange:(UITextView *)textView{
    if ([textView.text length] == 0) {
        [opinionPlaceholder setHidden:NO];
    }else{
        [opinionPlaceholder setHidden:YES];
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:NO];
}
#pragma mark collection -delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _nextNodeUserList.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UserLeaderCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier: leaderCellID forIndexPath:indexPath];
    cell.LeaderName.text =[_nextNodeUserList[indexPath.row] name];
    cell.LeaderName.font =[UIFont systemFontOfSize:11];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{    
    UserLeaderCell *cell =(UserLeaderCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (indexPath == nil) {
        cell.redSure.hidden =!cell.redSure.hidden;
        lastIndexPath =indexPath;
    }else if (indexPath != nil && lastIndexPath ==indexPath){
        cell.redSure.hidden =!cell.redSure.hidden;
    }else if (indexPath != nil && indexPath !=lastIndexPath){
        UserLeaderCell *lastcell =(UserLeaderCell *)[collectionView cellForItemAtIndexPath:lastIndexPath];
        lastcell.redSure.hidden =!lastcell.hidden;
        cell.redSure.hidden =!cell.redSure.hidden;
        lastIndexPath =indexPath;
    }
    if (cell.redSure.hidden ==NO) {
        _nextUserID =[_nextNodeUserList[indexPath.row] Id];
    }else{
        _nextUserID =nil;
    }
    
}

- (IBAction)commitClickedBtn:(UIButton *)sender {
    //判断反驳下备注是否为空
    if ([UITool isBlankString:self.opinionTextView.text] && self.isReject ==YES) {
        [MBProgressHUD showSuccess:@"请填写备注"];
        return;
    }
//    if ([UITool isBlankString:self.nextUserID]) {
//        [MBProgressHUD showSuccess:@"请选择审批人"];
//        return;
//    }
    NSString *strUrl;
    NSString *approvalStr;
    if (self.SAApprovalStatusType ==SAApprovalContract) {
        strUrl =[NSString stringWithFormat:SAPContractActionKUrl,_actionCode,_nextUserID,self.opinionTextView.text];
        approvalStr =@"合同";
    }else{
        strUrl =[NSString stringWithFormat:SAPTransferActionKUrl,_actionCode,_nextUserID,self.opinionTextView.text];
        approvalStr =@"调拨";
    }
    
    
    NSRange rang =[strUrl rangeOfString:@"&remark"];
    
    NSString *url;
    
    //判断审批状态下并且备注为空的时候
    if ([UITool isBlankString:self.opinionTextView.text]) {
        url =[strUrl substringToIndex:rang.location];
        NSLog(@"str3:%@",url);
    }else{
        url =[strUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    }
    

    [_http postTokenDataWithUrl:url httpBobyData:_detailDict onSuccess:^(NSData *data) {
        
        NSDictionary *dict =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",dict);
        
        if([dict[@"success"] integerValue] ==1 ){
            [MBProgressHUD showSuccess:[NSString stringWithFormat:@"%@%@成功",approvalStr,self.navtitle]];
            !self.completion ?: self.completion(nil, self);
            [self.navigationController popViewControllerAnimated:NO];
        }else{
            [MBProgressHUD showSuccess:[NSString stringWithFormat:@"%@%@失败",approvalStr,self.navtitle]];
        }
        
    } onFailure:^(NSError *error) {
        [MBProgressHUD showSuccess:[NSString stringWithFormat:@"%@%@失败",approvalStr,self.navtitle]];
    }];
   
}

- (IBAction)cancelClickedBtn:(UIButton *)sender {
    //!self.completion ?: self.completion(nil, self);
    [self.navigationController popViewControllerAnimated:NO];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
